<?
/**
 * Insert casting infomation to database
 * @inputs : (global)
 * @input $cc_date : casting date(DD-MM-YYYY) convert to timestamp 
 * @input $heat_no : from eaf heat no
 * @input $lmf_heat_no : from lmf heat no 
 */
 
/* This is how it should be done. */

class eafbox {
    var $name;
	var $tablename;
    var $pkeys  = array();
    var $fields = array();
    var $items  = array();

	//constructor
    function eafbox($nm = '') {
        $this->name = $nm;
	}

	function set_table_name($tbn) {
		$this->tablename = $tbn;
	}

	function add_pkey($pk) {
		$this->pkeys[] = $pk;
	}

	function add_field($fld) {
		$this->fields[] = $fld;
	}

	function add_item($itemid) {
		$this->items[] = $itemid;
	}

	function sql_insert() {
		$arr = array_merge($this->pkeys,$this->fields);
		$sql = "INSERT INTO $this->tablename ( " ;

		$sql2 = join($arr," ,");
		$sql .= $sql2 . " ) VALUES( ";

		for ($i=0;$i<count($arr);$i++) {
			$key = $arr[$i];
			$sql3_arr[] = "'$GLOBALS[$key]'";
		}
		$sql3 = join($sql3_arr," ,");
		$sql .= $sql3 . ")";

		$result = pg_query($sql);

		if($result) {
			$status = "OK";
		}
		else {
			$status = "ERROR";
		}
		return $status;
	}

}
/////////////////////////////////////////////////////////////////////////////////////////
class pbucket { //prepared bucket
    var $bucket_no;
    var $materials  = array();

	//constructor
    function pbucket($bn) {
        $this->bucket_no = $bn;
	}

	function add_material($itemid,$itemval) {
		$this->materials["$itemid"] = $itemval;
	}

	function display_detail() {
		$det = "";
		?>
<table width="150" border="1" cellpadding="0" cellspacing="0" class="bucket_box">
                <tr align="center"> 
                  <td colspan="2" bgcolor="#FFCC99"><strong>Bucket No:<?=$this->bucket_no?></strong></td>
                </tr>
		<?
		$j = 0;
		while(list($k,$v) = each($this->materials)){
?>
                <tr> 
                  <td width="50%" bgcolor="#CCCCCC"><strong>&nbsp;<?=$k?></strong></td>
                  <td align="center"><?=$v?></td>
                </tr>

<?
		
		}
?>
		<tr bgcolor="#cccccc" height="20"> 
            <td colspan="2" align="center"> <input name="button" type="button" class="detail_button" value=" Edit " onclick="GoEditPrepared(<?=$this->bucket_no?>)"> 
            </td>
          </tr>
<?
		echo "</table><br>";
	}//end function display_detail
	
}

class abucket extends pbucket { //heatno assigned bucket
	var $heat_no;

	//constructor
	function abucket($buckno,$heatno) {
		$this->bucket_no = $buckno;
		$this->heat_no   = $heatno;
	}

	function display_detail() {
		$det = "";
		?>
<table width="100" border="1" cellpadding="0" cellspacing="0" class="bucket_box">
                <tr align="center"> 
                  <td colspan="2" bgcolor="#999999"><strong>Heat No:<?=$this->heat_no?></strong></td>
                </tr>
                <tr align="center"> 
                  <td colspan="2" bgcolor="#FFCC99"><strong>Bucket No:<?=$this->bucket_no?></strong></td>
                </tr>
		<?
		$j = 0;
		while(list($k,$v) = each($this->materials)){
?>
                <tr> 
                  <td width="60%" bgcolor="#CCCCCC"><strong>&nbsp;<?=$k?></strong></td>
                  <td width="40%" align="center"><?=$v?></td>
                </tr>

<?
		
		}
?>

		<tr bgcolor="#999999" height="20"> 
            <td colspan="2" align="center"> <input name="button" type="button" class="detail_button" value=" Edit " onclick="GoEditStandby(<?=$this->bucket_no?>,<?=$this->heat_no?>)"> 
            </td>
          </tr>


<?
		echo "</table><br>";		
	}//end function display_detail


}//end class
/////////////////////////////////////////////////////////////////////////////////////////

function sqlEafHeatOperation($act) {
	global $cc_date,$heat_no, $lmf_heat_no , $ccm_no , $cc_seq_no , $cc_recorder , $cc_foreman , $cc_shift , $cc_steel_grade;
	global $cc_fly_seq1 , $cc_billet_size, $cc_prod_type ,$cc_cut_length ,$cc_remark;
	dbConn();

	switch ($act) {
		case "insert":
			$sql = "INSERT INTO cc_info_master(".
					"cc_date,".
					"heat_no,".
		 			"lmf_heat_no,".
		 			"ccm_no,".
		 			"cc_seq_no,".
		 			"cc_recorder,".
		 			"cc_foreman,".
		 			"cc_shift,".
		 			"cc_steel_grade,".
		 			"cc_fly_seq,".
		 			"cc_billet_size,".
		 			"cc_prod_type,".
		 			"cc_cut_length".
		  			") VALUES(".
			
					"'$cc_date',".
					"'$heat_no',".
		 			"'$lmf_heat_no',".
		 			"'$ccm_no',".
		 			"'$cc_seq_no',".
		 			"'$cc_recorder',".
		 			"'$cc_foreman',".
		 			"'$cc_shift',".
		 			"'$cc_steel_grade',".
		 			"'$cc_fly_seq',".
		 			"'$cc_billet_size',".
		 			"'$cc_prod_type',".
		 			"'$cc_cut_length'".
					")";

			$result = pg_query($sql);
			$stat = $result? "1":"0";
			break;
		
		case "update":
			$sql =  "UPDATE cc_info_master SET ".
					"cc_date='$cc_date',".
					"heat_no='$heat_no',".
					"lmf_heat_no='$lmf_heat_no',".
					"ccm_no='$ccm_no',".
					"cc_seq_no='$cc_seq_no',".
					"cc_recorder='$cc_recorder',".
					"cc_foreman='$cc_foreman',".
					"cc_shift='$cc_shift',".
					"cc_steel_grade='$cc_steel_grade',".
					"cc_fly_seq='$cc_fly_seq',".
					"cc_billet_size='$cc_billet_size',".
					"cc_prod_type='$cc_prod_type',".
					"cc_cut_length='$cc_cut_length' ".
					" WHERE heat_no='$heat_no'";

			$result = pg_query($sql);
			$stat = $result? "1":"0";
			break;
		
		case "delete":
			1;
			break;
	}
	return "$stat" . "\n";
}

/////////////////////////////////////////////////////////////////////////////////////////
function makeInput($boxtitle,$tblname,$unit='') {

	$dsp = "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" bgcolor=\"#ECE9D8\" class=\"heatoperation\">\n";
	$dsp.= "      <tr align=\"center\" bgcolor=\"#FFCC99\">\n";
	$dsp.= "        <td colspan=\"2\" nowrap class=\"heatoperationTitle\">$boxtitle</td>\n";
	$dsp.= "      </tr>\n";
	
	$sql = "SELECT * FROM $tblname";
	$result = pg_query($sql);
	$num_row = pg_num_rows($result);

	$i=0;
	while ($i<$num_row) {
		$arr = pg_fetch_array($result, $i, PGSQL_ASSOC);
		$fieldname = $arr[name];
		$fieldid   = $arr[id];
		
		$dsp.= "      <tr>\n";
		$dsp.= "        <td nowrap align=\"left\">&nbsp;&nbsp;".$fieldname."</td>\n";
		$dsp.= "        <td nowrap><input name=\"$fieldid\" type=\"text\" class=\"txtwakuWhite\" size=\"10\"> $unit</td>\n";
		$dsp.= "      </tr>\n";

		$i++;
	}
	
	$dsp.= "</table>";

	echo $dsp;

}

function makeInput2($boxtitle,$tblname,$unit='',$fldid,$fldname) {

	$dsp = "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" bgcolor=\"#ECE9D8\" class=\"heatoperation\">\n";
	$dsp.= "      <tr align=\"center\" bgcolor=\"#FFCC99\">\n";
	$dsp.= "        <td colspan=\"2\" nowrap class=\"heatoperationTitle\">$boxtitle</td>\n";
	$dsp.= "      </tr>\n";
	
	$sql = "SELECT * FROM $tblname";
	$result = pg_query($sql);
	$num_row = pg_num_rows($result);

	$i=0;
	while ($i<$num_row) {
		$arr = pg_fetch_array($result, $i, PGSQL_ASSOC);
		$fieldname = $arr[$fldname];
		$fieldid   = $arr[$fldid];
		
		$dsp.= "      <tr>\n";
		$dsp.= "        <td nowrap align=\"left\">&nbsp;&nbsp;".$fieldname."</td>\n";
		$dsp.= "        <td nowrap><input name=\"$fieldid\" type=\"text\" class=\"txtwakuWhite\" size=\"10\"> $unit</td>\n";
		$dsp.= "      </tr>\n";

		$i++;
	}
	
	$dsp.= "</table>";

	echo $dsp;

}

function makeInput3($boxtitle,$tblname,$unit='',$fldid,$fldname) {

	$dsp = "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" bgcolor=\"#ECE9D8\" class=\"heatoperation\">\n";
	$dsp.= "      <tr align=\"center\" bgcolor=\"#FFCC99\">\n";
	$dsp.= "        <td colspan=\"2\" nowrap class=\"heatoperationTitle\">$boxtitle</td>\n";
	$dsp.= "      </tr>\n";
	
	$sql = "SELECT * FROM $tblname";
	$result = pg_query($sql);
	$num_row = pg_num_rows($result);

	$i=0;
	while ($i<$num_row) {
		$arr = pg_fetch_array($result, $i, PGSQL_ASSOC);
		$fieldname = $arr[$fldname];
		$fieldid   = $arr[$fldid];

		$value = $GLOBALS["$fieldid"];
		
		$dsp.= "      <tr>\n";
		$dsp.= "        <td nowrap align=\"left\">&nbsp;&nbsp;".$fieldname."</td>\n";
		$dsp.= "        <td nowrap><input name=\"$fieldid\" id=\"$fieldid\" value=\"$value\" type=\"text\" class=\"inputTextBox\" size=\"10\" onfocus=\"onEnter(this)\" onblur=\"onExit(this)\"> $unit</td>\n";
		$dsp.= "      </tr>\n";

		$i++;
	}
	
	$dsp.= "</table>";

	echo $dsp;
}

function makeEditScrap($boxtitle,$tblname,$unit='',$fldid,$fldname,$oldval) {

	$dsp = "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" bgcolor=\"#ECE9D8\" class=\"heatoperation\">\n";
	$dsp.= "      <tr align=\"center\" bgcolor=\"#FFCC99\">\n";
	$dsp.= "        <td colspan=\"2\" nowrap class=\"heatoperationTitle\">$boxtitle</td>\n";
	$dsp.= "      </tr>\n";
	
	$sql = "SELECT * FROM $tblname";
	$result = pg_query($sql);
	$num_row = pg_num_rows($result);

	//$oldval['locals'] = 10;
	//$oldval['hms'] = 20;
	
	$i=0;
	while ($i<$num_row) {
		$arr = pg_fetch_array($result, $i, PGSQL_ASSOC);
		$fieldname = $arr[$fldname];
		$fieldid   = $arr[$fldid];

		$value = $GLOBALS["$fieldid"];
		
		$dsp.= "      <tr>\n";
		$dsp.= "        <td nowrap align=\"left\">&nbsp;&nbsp;".$fieldname."</td>\n";
		
		if ($oldval[$fieldid]!="") $value=$oldval[$fieldid];
		$dsp.= "        <td nowrap><input name=\"$fieldid\" id=\"$fieldid\" value=\"$value\" type=\"text\" class=\"inputTextBox\" size=\"10\" onfocus=\"onEnter(this)\" onblur=\"onExit(this)\"> $unit</td>\n";
		$dsp.= "      </tr>\n";

		$i++;
	}
	
	$dsp.= "</table>";

	echo $dsp;
}

function makektjet($boxtitle,$tblname,$unit='',$fldid,$fldname) {

	$dsp = "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" bgcolor=\"#ECE9D8\" class=\"heatoperation\">\n";
	$dsp.= "      <tr align=\"center\" bgcolor=\"#FFCC99\">\n";
	$dsp.= "        <td colspan=\"3\" nowrap class=\"heatoperationTitle\">$boxtitle</td>\n";
	$dsp.= "      </tr>\n";
	
	$sql = "SELECT * FROM $tblname";
	$result = pg_query($sql);
	$num_row = pg_num_rows($result);

	$i=0;
	while ($i<$num_row) {
		$arr = pg_fetch_array($result, $i, PGSQL_ASSOC);
		$fieldname = $arr[$fldname];
		$fieldid   = $arr[$fldid];

		$value = $GLOBALS["$fieldid"];
		
		$dsp.= "      <tr>\n";
		$dsp.= "        <td nowrap align=\"left\">&nbsp;&nbsp;".$fieldname."</td>\n";
		$dsp.= "        <td nowrap valign=\"middle\"><input name=\"time_hour\" type=\"text\" class=\"inputDayBox\" id=\"time_hour\" size=\"1\" maxlength=\"2\" value=\"$time_hour\" onfocus=\"onEnter(this)\" onblur=\"if(this.value>23) { alert('Invalid Time(hour)');this.select();return 0;} onExit(this);\">:<input name=\"time_minute\" type=\"text\" class=\"inputDayBox\" id=\"time_minute\" size=\"1\" maxlength=\"2\" value=\"$time_hour\" onfocus=\"onEnter(this)\" onblur=\"if(this.value>23) { alert('Invalid Time(hour)');this.select();return 0;} onExit(this);\"></td>\n";
		$dsp.= "        <td nowrap><input name=\"Button22\" type=\"button\" class=\"bigBtn75\" onClick=\"GoCancel()\" value=\"OFF\"></td>\n";
		$dsp.= "      </tr>\n";

		$i++;
	}
	
	$dsp.= "</table>";

	echo $dsp;
}

function makeCheckFormat($tblname,$fldid,$fldname) {
	$sql = "SELECT * FROM $tblname";
	$result = pg_query($sql);
	$num_row = pg_num_rows($result);

	$i=0;
	while ($i<$num_row) {
		$arr = pg_fetch_array($result, $i, PGSQL_ASSOC);
		$fieldname = $arr[$fldname];
		$fieldid   = $arr[$fldid];

		$dsp.= "        if (!checkInput(floatRegExp,document.getElementById('$fieldid'),'$fieldname')) return false;\n";

		$i++;
	}
	
	$dsp.= "\n";
	echo $dsp;

}


function makeCheckFormat2($tblname,$fldid,$fldname) {
	$sql = "SELECT * FROM $tblname";
	$result = pg_query($sql);
	$num_row = pg_num_rows($result);

	$i=0;
	while ($i<$num_row) {
		$arr = pg_fetch_array($result, $i, PGSQL_ASSOC);
		$fieldname = $arr[$fldname];
		$fieldid   = $arr[$fldid];

		//$dsp.= "        if (!checkInput(floatRegExp,document.getElementById('$fieldid'),'$fieldname')) return false;\n";
		$dsp.= "        if (!checkFormat(floatRegExp,document.getElementById('$fieldid'),'$fieldname')) return false;\n";
	  
		$i++;
	}
	
	$dsp.= "\n";
	echo $dsp;

}




function editInputBucket($boxtitle,$tblname,$unit='',$fldid,$fldname,$hn,$bc) {

	$dsp = "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" bgcolor=\"#ECE9D8\" class=\"heatoperation\">\n";
	$dsp.= "      <tr align=\"center\" bgcolor=\"#FFCC99\">\n";
	$dsp.= "        <td colspan=\"2\" nowrap class=\"heatoperationTitle\">$boxtitle</td>\n";
	$dsp.= "      </tr>\n";
	
	//MAKE INPUT TABLA
	$sql = "SELECT * FROM $tblname";
	$result = pg_query($sql);
	$num_row = pg_num_rows($result);

	//SELECT EXISTING VALUES TO EDIT
	
	$i=0;
	while ($i<$num_row) {
		$arr = pg_fetch_array($result, $i, PGSQL_ASSOC);
		$fieldname = $arr[$fldname];
		$fieldid   = $arr[$fldid];
		
		$qqq = "select * from bucket_material where bucket_material_id='$fieldid' and heat_no='$hn' and bucket_cnt='$bc'";
		$ee = pg_query($qqq);
		$material_array = pg_fetch_array($ee);
		$material = trim($material_array['bucket_material_weight']);

		$dsp.= "      <tr>\n";
		$dsp.= "        <td nowrap align=\"left\">&nbsp;&nbsp;".$fieldname."</td>\n";
		$dsp.= "        <td nowrap><input name=\"$fieldid\" type=\"text\" class=\"inputTextBox\" size=\"10\" value=\"$material\" onfocus=\"onEnter(this)\" onblur=\"onExit(this)\"> $unit </td>\n";
		$dsp.= "      </tr>\n";

		$i++;
	}
	
	$dsp.= "</table>";

	echo $dsp;

}

function convert2pgdate($date) {
	return substr($date,6,4)."-".substr($date,3,2)."-".substr($date,0,2);
}

function convert2msiandate($date) {
	return substr($date,8,2)."-".substr($date,5,2)."-".substr($date,0,4);
}
?>